package com.lihh.rocket5.normal;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

/**
 * 以 同步的方式发送消息
 *
 * @author lihh
 */
public class SyncProducer {
  public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException, UnsupportedEncodingException {
    DefaultMQProducer producer = new DefaultMQProducer("group_test");
    // 设置NameServer 的地址
    producer.setNamesrvAddr("localhost:9876");
    // 启动 producer 实例
    producer.start();
    
    // 开始发送消息
    for (int i = 0; i < 10; i++) {
      Message message = new Message("order001", "TagA", ("hello mq " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
      // 发送消息 到 broker
      SendResult send = producer.send(message);
      System.out.printf("%s%n", send);
    }
    
    producer.shutdown();
  }
}
